package com.zipon.tpf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.xiaoleilu.hutool.date.DateUtil;
import com.zipon.tpf.entity.TpfBugInfo;
import com.zipon.tpf.service.TpfBugInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * bug信息表  前端控制器
 * </p>
 *
 * @author zipon
 * @since 2018-08-26
 */
@Controller
@RequestMapping("/tpf/tpfBugInfo")
public class TpfBugInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TpfBugInfoController.class);

    @Autowired
    private TpfBugInfoService tpfBugInfoService;

    @GetMapping("list")
    @SysLog("跳转bug信息表列表")
    public String list(){
        return "/tpf/tpfBugInfo/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求bug信息表列表数据")
    public LayerData<TpfBugInfo> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<TpfBugInfo> layerData = new LayerData<>();
        EntityWrapper<TpfBugInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String title = (String) map.get("title");
            if(StringUtils.isNotBlank(title)) {
                wrapper.like("title",title);
            }else{
                map.remove("title");
            }

            String creator = (String) map.get("creator");
            if(StringUtils.isNotBlank(creator)) {
                wrapper.eq("creator",creator);
            }else{
                map.remove("creator");
            }

            String beginCreateTime = (String) map.get("beginCreateTime");
            String endCreateTime = (String) map.get("endCreateTime");
            if(StringUtils.isNotBlank(beginCreateTime)) {
                Date begin = DateUtil.parse(beginCreateTime);
                wrapper.ge("create_time",begin);
            }else{
                map.remove("beginCreateTime");
            }
            if(StringUtils.isNotBlank(endCreateTime)) {
                Date end = DateUtil.parse(endCreateTime);
                wrapper.le("create_time",end);
            }else{
                map.remove("endCreateTime");
            }

            String responsibility = (String) map.get("responsibility");
            if(StringUtils.isNotBlank(responsibility)) {
                wrapper.eq("responsibility",responsibility);
            }else{
                map.remove("responsibility");
            }

            String director = (String) map.get("director");
            if(StringUtils.isNotBlank(director)) {
                wrapper.eq("director",director);
            }else{
                map.remove("director");
            }

        }
        Page<TpfBugInfo> pageData = tpfBugInfoService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增bug信息表页面")
    public String add(){
        return "/tpf/tpfBugInfo/add";
    }

    @PostMapping("add")
    @SysLog("保存新增bug信息表数据")
    @ResponseBody
    public RestResponse add(TpfBugInfo tpfBugInfo){
        if(StringUtils.isBlank(tpfBugInfo.getTitle())){
            return RestResponse.failure("bug标题不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getDetail())){
            return RestResponse.failure("bug详情不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getCreator())){
            return RestResponse.failure("30不能为空");
        }
        if(tpfBugInfo.getCreateTime() == null){
            return RestResponse.failure("创建时间不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getResponsibility())){
            return RestResponse.failure("责任人不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getDirector())){
            return RestResponse.failure("指派给不能为空");
        }
        tpfBugInfoService.insert(tpfBugInfo);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑bug信息表页面")
    public String edit(Long id,Model model){
        TpfBugInfo tpfBugInfo = tpfBugInfoService.selectById(id);
        model.addAttribute("tpfBugInfo",tpfBugInfo);
        return "/tpf/tpfBugInfo/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑bug信息表数据")
    public RestResponse edit(TpfBugInfo tpfBugInfo){
        if(null == tpfBugInfo.getId() || 0 == tpfBugInfo.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getTitle())){
            return RestResponse.failure("bug标题不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getDetail())){
            return RestResponse.failure("bug详情不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getCreator())){
            return RestResponse.failure("30不能为空");
        }
        if(tpfBugInfo.getCreateTime() == null){
            return RestResponse.failure("创建时间不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getResponsibility())){
            return RestResponse.failure("责任人不能为空");
        }
        if(StringUtils.isBlank(tpfBugInfo.getDirector())){
            return RestResponse.failure("指派给不能为空");
        }
        tpfBugInfoService.updateById(tpfBugInfo);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除bug信息表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        TpfBugInfo tpfBugInfo = tpfBugInfoService.selectById(id);
        tpfBugInfo.setDelFlag(true);
        tpfBugInfoService.updateById(tpfBugInfo);
        return RestResponse.success();
    }

}